package com.ruoyi.develop.finance.financeBuyInvoice.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.finance.financeAp.domain.FinanceAp;
import com.ruoyi.develop.finance.financeAp.service.IFinanceApService;
import com.ruoyi.develop.finance.financeBuyInvoiceStoreRel.domain.FinanceBuyInvoiceStoreRel;
import com.ruoyi.develop.finance.financeBuyInvoiceStoreRel.service.IFinanceBuyInvoiceStoreRelService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.finance.financeBuyInvoice.domain.FinanceBuyInvoice;
import com.ruoyi.develop.finance.financeBuyInvoice.service.IFinanceBuyInvoiceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购发票Controller
 * @author lmm
 * @date 2025-07-24
 */
@RestController
@RequestMapping("/financeBuyInvoice/financeBuyInvoice")
public class FinanceBuyInvoiceController extends BaseController {
    @Autowired
    private IFinanceBuyInvoiceService financeBuyInvoiceService;
    @Autowired
    private IFinanceBuyInvoiceStoreRelService financeBuyInvoiceStoreRelService;
    @Autowired
    private IFinanceApService financeApService;



    /**
     * 查询采购发票列表
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoice:query')")
    @GetMapping("/list")
    public TableDataInfo list(FinanceBuyInvoice entity) {
        startPage();
        List<FinanceBuyInvoice> list = financeBuyInvoiceService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出采购发票列表
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoice:export')")
    @Log(title = "采购发票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FinanceBuyInvoice entity) {
        List<FinanceBuyInvoice> list = financeBuyInvoiceService.selectList(entity);
        ExcelUtil<FinanceBuyInvoice> util = new ExcelUtil<FinanceBuyInvoice>(FinanceBuyInvoice. class);
        util.exportExcel(response, list, "采购发票数据");
    }

    /**
     * 获取采购发票详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        FinanceBuyInvoice entity = financeBuyInvoiceService.selectById(id);
        FinanceBuyInvoiceStoreRel relParams = new FinanceBuyInvoiceStoreRel();
        relParams.setInvoiceId(entity.getId());
        List<FinanceBuyInvoiceStoreRel> relList = financeBuyInvoiceStoreRelService.selectList(relParams);
        entity.setInvoiceRukuRelList(relList);
        return AjaxResult.success(entity);
    }

    /**
     * 新增采购发票
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoice:add')")
    @Log(title = "采购发票", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FinanceBuyInvoice entity) {
        int i = financeBuyInvoiceService.insert(entity);
        List<FinanceBuyInvoiceStoreRel> relList = entity.getInvoiceRukuRelList();
        for (FinanceBuyInvoiceStoreRel rel : relList) {
            rel.setInvoiceId(entity.getId());
            rel.setInvoiceCode(entity.getInvoiceCode());
            rel.setStatus(BusinessConstants.STATUS_YES);
            financeBuyInvoiceStoreRelService.update(rel);
        }
        return toAjax(i);
    }

    /**
     * 修改采购发票
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoice:edit')")
    @Log(title = "采购发票", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody FinanceBuyInvoice entity) {
        int i = financeBuyInvoiceService.update(entity);
        List<FinanceBuyInvoiceStoreRel> relList = entity.getInvoiceRukuRelList();
        for (FinanceBuyInvoiceStoreRel rel : relList) {
            rel.setInvoiceId(entity.getId());
            rel.setInvoiceCode(entity.getInvoiceCode());
            rel.setStatus(BusinessConstants.STATUS_YES);
            financeBuyInvoiceStoreRelService.update(rel);
        }
        return toAjax(i);
    }

    /**
     * 删除采购发票
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoice:remove')")
    @Log(title = "采购发票", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(financeBuyInvoiceService.deleteByIds(ids));
    }

    /**
     * 提交采购发票
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoice:remove')")
    @Log(title = "采购发票", businessType = BusinessType.DELETE)
    @GetMapping("/submit/{ids}")
    public AjaxResult submit(@PathVariable String[] ids) {
        int i = 0;
        for (String id : ids) {
            FinanceBuyInvoice entity = financeBuyInvoiceService.selectById(id);
            entity.setStatus(BusinessConstants.BUY_INVOICE_STATUS_SUBMIT);
            financeBuyInvoiceService.update(entity);
            i++;
        }
        return toAjax(i);
    }

    /** * 审核退回 */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoice:edit')")
    @Log(title = "采购发票", businessType = BusinessType.UPDATE)
    @PutMapping("/assessReject")
    public AjaxResult assessReject(@RequestBody FinanceBuyInvoice entity) {
        FinanceBuyInvoice buyInvoice = financeBuyInvoiceService.selectById(entity.getId());
        if (buyInvoice == null) {
            return AjaxResult.error("该单据不存在");
        }
        if (BusinessConstants.BUY_INVOICE_STATUS_REJECT.equals(buyInvoice.getStatus())) {
            return AjaxResult.error("该发票已经审核退回，请核对！");
        }
        if (BusinessConstants.BUY_INVOICE_STATUS_PASS.equals(buyInvoice.getStatus())) {
            return AjaxResult.error("该发票已经审核通过，请核对！");
        }
        if (!(BusinessConstants.BUY_INVOICE_STATUS_SUBMIT.equals(buyInvoice.getStatus()) )) {
            return AjaxResult.error("该发票状态异常，请核对！");
        }
        entity.setAssessTime(DateUtils.getNowDate());
        entity.setAssessBy(SecurityUtils.getUsername());
        entity.setStatus(BusinessConstants.BUY_INVOICE_STATUS_REJECT);
        return toAjax(financeBuyInvoiceService.update(entity));
    }

    /**
     * 审核通过
     */
    @PreAuthorize("@ss.hasPermi('develop:FinanceBuyInvoice:edit')")
    @Log(title = "采购发票", businessType = BusinessType.UPDATE)
    @Transactional
    @PutMapping("/assessPass")
    public AjaxResult assessPass(@RequestBody FinanceBuyInvoice entity) {
        FinanceBuyInvoice buyInvoice = financeBuyInvoiceService.selectById(entity.getId());
        if (buyInvoice == null) {
            return AjaxResult.error("该单据不存在");
        }
        if (BusinessConstants.BUY_INVOICE_STATUS_REJECT.equals(buyInvoice.getStatus())) {
            return AjaxResult.error("该发票已经审核退回，请核对！");
        }
        if (BusinessConstants.BUY_INVOICE_STATUS_PASS.equals(buyInvoice.getStatus())) {
            return AjaxResult.error("该发票已经审核通过，请核对！");
        }
        if (!(BusinessConstants.BUY_INVOICE_STATUS_SUBMIT.equals(buyInvoice.getStatus()) )) {
            return AjaxResult.error("该发票状态异常，请核对！");
        }
        entity.setAssessTime(DateUtils.getNowDate());
        entity.setAssessBy(SecurityUtils.getUsername());
        entity.setStatus(BusinessConstants.BUY_INVOICE_STATUS_PASS);
        // 审核通过之后进入 应收 表
        financeApService.addByInvoice(buyInvoice);
        return toAjax(financeBuyInvoiceService.update(entity));
    }











}
